package com.servlet;

import com.entity.Foods;
import com.entity.Like;
import com.service.FoodsService;
import com.service.TypeService;
import com.service.ShopService;
import com.service.impl.FoodsServiceImpl;
import com.service.impl.ShopServiceImpl;
import com.service.impl.TypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet("/shop.let")
public class ShopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        String type = req.getParameter("type");
        ShopService shopService=new ShopServiceImpl();
        FoodsService foodsService=new FoodsServiceImpl();
        int pageCount=8;
        switch (type){
            case "findShop":
                if (session.getAttribute("user") == null) {
                    out.println("<script>alert('请登录');parent.window.location.href='home-1.jsp';</script>");
                    return;
                }


                int pageIndex = Integer.parseInt( req.getParameter("pageIndex"));
                int pagesize=shopService.getCount();
                if(pageIndex<1){
                    pageIndex=1;
                }
                if (pagesize / 8 != 0) {
                    pagesize=pagesize/8+1;
                }else {
                    pagesize=pageCount/8;
                }
                if(pageIndex>pagesize){
                    pageIndex=pagesize;
                }
                //查询所有食物转发页面
                List<Map<String, Object>> listtype = shopService.foodFindAll();
                List<Map<String, Object>> list = shopService.shopFind(pageIndex,pageCount);
                List<Map<String, Object>> likeDesc = shopService.lisLikeFour();
                List<Like> loves=foodsService.findUserLove();
                req.setAttribute("list", list);
                session.setAttribute("listtype", listtype);
                session.setAttribute("likeDesc", likeDesc);
                req.setAttribute("pagesize", pagesize);
                session.setAttribute("loves", loves);
                req.getRequestDispatcher("shop.jsp").forward(req,resp);
                break;
            case "listPrice":
                pageIndex = Integer.parseInt( req.getParameter("pageIndex"));
                pagesize=shopService.getCount();
                if(pageIndex<1){
                    pageIndex=1;
                }
                if (pagesize / 8 != 0) {
                    pagesize=pagesize/8+1;
                }else {
                    pagesize=pageCount/8;
                }
                if(pageIndex>pagesize){
                    pageIndex=pagesize;
                }
                List<Map<String, Object>> listPice = shopService.listPrice(pageIndex, pageCount);
                req.setAttribute("listPice", listPice);
                req.setAttribute("pagesize", pagesize);
                req.getRequestDispatcher("shop.jsp").forward(req,resp);
                break;
            case "listPriceDesc":
                pageIndex = Integer.parseInt( req.getParameter("pageIndex"));
                pagesize=shopService.getCount();
                if(pageIndex<1){
                    pageIndex=1;
                }
                if (pagesize / 8 != 0) {
                    pagesize=pagesize/8+1;
                }else {
                    pagesize=pageCount/8;
                }
                if(pageIndex>pagesize){
                    pageIndex=pagesize;
                }
                List<Map<String, Object>> listPriceDesc = shopService.listPriceDesc(pageIndex, pageCount);
                req.setAttribute("listPriceDesc", listPriceDesc);
                req.setAttribute("pagesize", pagesize);
                req.getRequestDispatcher("shop.jsp").forward(req,resp);
                break;
            case "listLike":
                pageIndex = Integer.parseInt( req.getParameter("pageIndex"));
                pagesize=shopService.getCount();
                if(pageIndex<1){
                    pageIndex=1;
                }
                if (pagesize / 8 != 0) {
                    pagesize=pagesize/8+1;
                }else {
                    pagesize=pageCount/8;
                }
                if(pageIndex>pagesize){
                    pageIndex=pagesize;
                }
                List<Map<String, Object>> listLike = shopService.lisLikeDesc(pageIndex, pageCount);
                req.setAttribute("listLike", listLike);
                req.setAttribute("pagesize", pagesize);
                req.getRequestDispatcher("shop.jsp").forward(req,resp);
                break;
            case "findPrice":
                int max = Integer.parseInt( req.getParameter("max"));
                int min = Integer.parseInt( req.getParameter("min"));
                pageIndex = Integer.parseInt( req.getParameter("pageIndex"));
                pagesize=shopService.getPriceCount(max,min);
                if(pageIndex<1){
                    pageIndex=1;
                }
                if (pagesize / 8 != 0) {
                    pagesize=pagesize/8+1;
                }else {
                    pagesize=pageCount/8;
                }
                if(pageIndex>pagesize){
                    pageIndex=pagesize;
                }
                List<Map<String, Object>> findPrice = shopService.findPrice(max, min, pageIndex, pageCount);
                req.setAttribute("findPrice", findPrice);
                req.setAttribute("pagesize", pagesize);
                req.getRequestDispatcher("shop.jsp").forward(req,resp);
                break;
            case "findName":
                break;
        }

    }
}
